let width = 45;
let requestUrl = null;

function addHandler() {
	requestUrl = "/partner_skill_category/insert";

	$("#skill_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#partner_skill_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/partner_skill_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);

		$("#skill_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

function imageHandler() {
	let rowData = $("#partner_skill_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/partner_skill_category/updateImageById";

		$("#skill_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

function distributeHandler() {
	$("#partner_list").datalist("reload", {
		callback: function () {
			$("#partner_list").datalist("selectRecord", 1);
		}
	});

	$("#partner_skill").dialog("open");
}

function deleteHandler() {
	let rowData = $("#partner_skill_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/partner_skill_category/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#partner_skill_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录", "info");
	}
}

$(document).ready(function() {

	$("#_name_").textbox({
		width: 150,
		prompt: "输入技能名称模糊查询"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).on("click", function() {
		let name = $("#_name_").textbox("getValue");

		$("#partner_skill_list").datagrid("reload", {
			name: name
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).on("click", function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 200,
		required: true
	});

	$("#note").textbox({
		width: 200,
		height: 100,
		required: true,
		multiline: true,
		onChange: function (value) {
			if (value) {
				let start = value.indexOf("【");
				let end = value.indexOf("】");

				if (start !== -1) {
					let name = value.substr(start + 1, end - 1);
					let note = value.substr(end + 1, value.length);

					$("#name").textbox("setValue", name);
					$("#note").textbox("setValue", note);
				}
			}
		}
	});
	
	$("#skill_dialog").dialog({
		title: "助战技能",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#skill_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#skill_dialog").dialog("close");
						$("#partner_skill_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#skill_dialog").dialog("close");
				$("#skill_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#partner_skill_list", "/partner_skill_category/upload");

	fetchUploadDialog("/partner_skill_category/updateImageById", function () {
		$("#partner_skill_list").datagrid("reload");
	});

	/******************************************************************************************************/

	$("#partner_type").combobox({
		url: "/partner_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: false,
		panelHeight: "auto",
		onSelect: function (record) {
			if (record) {
				$("#partner_list").datalist("reload", {
					type: record.id
				});
			}
		}
	});

	$("#partner_list").datalist({
		url: "/partner/selectByType",
		idField: "id",
		valueField: "id",
		textField: "name",
		width: 180,
		height: 380,
		textFormatter: function (value, rowData, rowIndex) {
			let html = "<table>";

			html += 	"<tr>";
			html += 		"<td rowspan='2'>";
			html += 			"<img height='45' src='" + rowData.image + "' />";
			html += 		"</td>";

			html += 		"<td>";
			html += 			"<span>" + rowData.name + "</span>";
			html += 		"</td>";
			html += 	"</tr>";

			html += 	"<tr>";
			html += 		"<td>";
			html += 			"<div id='type_" + rowIndex + "'></div>";
			html += 		"</td>";
			html += 	"</tr>";
			html += "</table>";

			ajaxGet("/partner_type/selectById", {
				id: rowData.type
			}, function (resp) {
				let data = resp.data;

				$("#type_" + rowIndex).html(data.name)
			}, error);

			return html;
		},
		onSelect: function(rowIndex, rowData) {
			if (rowData) {
				let partnerId = rowData.id;

				$("#partnerId").val(partnerId);

				$("#skill_list").treegrid("reload", {
					partnerId: partnerId
				});
			}
		}
	});

	$("#skill_list").treegrid({
		url: "/partner_skill/listTree",
		method: "get",
		idField: "id",
		treeField: "name",
		width: 635,
		height: 410,
		fitColumns: true,
		columns:[[
			{title: "技能名称", field: "name", align: "left", width: 150},
			{title: "技能图片", field: "image", align: "center", width: 60
				, formatter: function(value) {
					if(value) {
						return "<img alt='暂无图片' height='30' src='" + value + "' />";
					}
				}
			},
			{title: "技能说明", field: "note", align: "left", width: 400,
				formatter: function(value) {
					if (value) {
						return "<div class='ell' title='" + value + "'>" + value + "</div>";
					}
				}
			}
		]],
		onContextMenu: function (e, rowData) {
			let name = rowData.name;

			if (rowData.children && name.includes("技能")) {
				$("#type").val(name.includes("基础") ? 0 : 1);
			}

			e.preventDefault();

			// 显示右键菜单
			$("#mm").menu("show", {
				left: e.pageX,
				top: e.pageY
			});
		}
	});

	$("#mm-insert").on("click", function() {
		let row = $("#skill_list").treegrid("getSelected");

		// 二级分类
		if (row.children && row.name.includes("技能")) {
			ajaxGet("/partner_skill/selectSkillIds", {
				type: $("#type").val(),
				partnerId: $("#partnerId").val()
			}, function (resp) {
				let data = resp.data;
				let list = $("#skillList");

				list.datalist("reload");
				list.datalist("unselectAll");

				if (data && data.length > 0) {
					for (let i = 0; i < data.length; i++) {
						list.datalist("selectRecord", data[i]);
					}
				}

				$("#skill").dialog("open");
			}, error);
		} else {
			unopen();
		}
	});

	$("#mm-update").on("click", function() {
		unopen();
	});

	$("#mm-delete").on("click", function() {
		let row = $("#skill_list").treegrid("getSelected");
		let partner = $("#partner_list").treegrid("getSelected");
		let message = "是否确认删除助战<span class='blue'>" + partner.name
			+ "</span>的<span class='green'>" + row.name + "</span>技能？";

		if (!row.children) {
			$.messager.confirm("提示", message, function(bool) {
				if (bool) {
					ajaxPost("/partner_skill/delete", {
						skillId: row.id,
						partnerId: partner.id
					}, function(resp) {
						showMsg(resp.message);

						$("#skill_list").treegrid("reload");
					}, error);
				}
			});
		} else {
			unopen();
		}
	});

	$("#partner_skill").dialog({
		title: '助战技能',
		modal: true,
		closed: true,
		closable: true,
		draggable: false
	});

	/******************************************************************************************************/

	$("#skillName").searchbox({
		width: 198,
		prompt: "输入技能名称",
		searcher: function(keyword){
			$("#skillList").datalist("reload", {
				name: keyword
			});
		}
	});

	$("#skillList").datalist({
		url: "/partner_skill_category/selectList",
		idField: "id",
		valueField: "id",
		textField: "name",
		width: 800,
		height: 400,
		lines: true,
		checkbox: true,
		singleSelect: false,
		checkOnSelect: true,
		selectOnCheck: true,
		textFormatter: function (value, rowData, rowIndex) {
			let content = "";

			content += 	"<table>";
			content += 		"<tr>";
			content += 			"<td rowspan='2'>";
			content += 				"<img height='40' src='" + rowData.image + "' />";
			content += 			"</td>";

			content += 			"<td>";
			content += 				"<span>" + rowData.name + "</span>";
			content += 			"</td>";
			content += 		"</tr>";

			content += 		"<tr>";
			content += 			"<td>";
			content += 				"<div>" + rowData.note + "</div>";
			content += 			"</td>";
			content += 		"</tr>";
			content += "</table>";

			return content;
		}
	});

	$("#skill").dialog({
		title: "助战技能",
		modal: false,
		buttons: [{
			iconCls: "icon-ok",
			text: "确认",
			handler: function() {
				let rows = $("#skillList").datalist("getChecked");

				if (rows && rows.length > 0) {
					let skillIds = [];

					for (let i = 0; i < rows.length; i++) {
						let data = rows[i];

						skillIds.push(data.id);
					}

					ajaxPost("/partner_skill/distribute", {
						type: $("#type").val(),
						partnerId: $("#partnerId").val(),
						skillIds: JSON.stringify(skillIds)
					}, function (response) {
						showMsg(response.message);

						$("#skill").dialog("close");
						$("#skill_list").treegrid("reload");
					}, error);
				}
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#partner_skill_list").datagrid({
		url: "/partner_skill_category/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-ok",
			text: "分配",
			handler: function() {
				distributeHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", title: "编号", align: "center"},
			{field: "name", title: "技能名称", align: "center", width: 50},
			{field: "image", title: "技能图片", align: "center", width: 50
				, formatter: function(value) {
					return "<img alt='暂无图片' width='" + size + "' src='" + value + "' />";
				}
			},
			{field: "note", title: "技能简介", align: "center", width: 200
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			}
		]]
	});

});